<?php

define('CITY_BAO', TRUE);
define("APPLICATION_ENV", 'product'); //发布产品后更改为product测试时development
define('CB_CORE', str_replace("\\", '/', dirname(__FILE__)) . '/');
define('CB_ROOT', dirname(CB_CORE) . '/');
define('CB_DATA', CB_ROOT . 'data/');
define('CB_CACHE', CB_DATA . 'cache/');
define('CB_INCLUDE', CB_CORE . 'include/');
define('CB_PLUGINS', CB_CORE . 'plugins/');
define('CB_LIBRARY', CB_CORE . 'library/');

if (file_exists(CB_ROOT . 'config.php')) {
    require_once (CB_ROOT . 'config.php');
} else {
    if (!file_exists(CB_DATA . 'install.lock')) {
        // 程序未安装，转到程序安装路径
        header('location: install');
        exit ();
    } else {
        die('配置文件config.php未找到!');
    }
}

header("Content-type: text/html; charset=" . CHARSET);

if (!defined('CB_MEMORY_LIMIT'))
    define('CB_MEMORY_LIMIT', '32M');

if (function_exists('memory_get_usage') && ((int) @ini_get('memory_limit') < abs(intval(CB_MEMORY_LIMIT))))
    @ini_set('memory_limit', CB_MEMORY_LIMIT);

set_magic_quotes_runtime(0);
@ini_set('magic_quotes_sybase', 0);

if (function_exists('date_default_timezone_set'))
    date_default_timezone_set(TIME_ZONE);

require_once CB_INCLUDE . 'error.class.php';
require_once CB_INCLUDE . 'core.func.php';
require_once CB_INCLUDE . 'api/plugin.php';
require_once CB_INCLUDE . 'default-filters.php';
//引入初始化函数库
require_once CB_INCLUDE . 'init.func.php';

cb_check_php_mysql_versions();

cb_unregister_GLOBALS ();

cb_fix_PHP_ENV ();

favicon_request ();

if (defined('CACHE_TYPE') && file_exists(CB_INCLUDE . 'cache/' . CACHE_TYPE . 'cache.class.php')) {
    if (strtolower(CACHE_TYPE) == 'file') {
        define('CACHE_PATH', CB_DATA . 'cache');
    }
    @include CB_INCLUDE . 'cache/' . CACHE_TYPE . 'cache.class.php';
} else {
    require_once CB_INCLUDE . 'cache/memorycache.class.php';
}
require CB_INCLUDE . 'api/cache.php';

cb_cache_init ();

cb_init_config_core();
if ($config_core['gzip_enable']) {
    ob_start('ob_gzip');
}
define('COOKIE_PREFIX', $config_core['cookie_prefix']);
define('COOKIE_DOMAIN', $config_core['cookie_domain']);
define('SECURE_AUTH_KEY', $config_core['secure_auth_key']);
//你可以在管理后台中启用调试模式
if ($config_core['debug']) {
    define('CB_DEBUG', true);
    if (defined('E_DEPRECATED'))
        error_reporting(E_ALL & ~ E_DEPRECATED & ~ E_STRICT);
    else
        error_reporting(E_ALL);
    // 你可以在配置文件config.php中添加 define('CB_DEBUG_DISPLAY', false); 来启用错误显示
    if (!defined('CB_DEBUG_DISPLAY') || CB_DEBUG_DISPLAY)
        ini_set('display_errors', 1);
    // 你可以在配置文件config.php中添加 define('CB_DEBUG_LOG', true); 来开启记录调试信息到 CB_DATA/logs/debug.log
    if (defined('CB_DEBUG_LOG') && CB_DEBUG_LOG) {
        ini_set('log_errors', 1);
        ini_set('error_log', CB_DATA . 'logs/debug.log');
    }
} else {
    define('CB_DEBUG', false);
    if (defined('E_RECOVERABLE_ERROR'))
        error_reporting(E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR);
    else
        error_reporting(0);
}
if ($config_core['nocacheheaders']) {
    nocache_headers();
}
define('TIME_STAMP', time());
define('IP_ADDRESS', getip());

timer_start ();

$cbdb = null;
cb_init_db ();
if (!empty($cbdb->error))
    dead_db ();

$prefix = $cbdb->set_prefix(DB_TABLE_PREFIX);
if (is_cb_error($prefix)) {
    cb_die('非法的数据表前缀！前缀只能由字母、数字和下划线组成，且以字母开头', '数据库初始化错误');
}
unset($prefix);

cb_init_session();

cb_init_template();

require CB_INCLUDE . 'request.class.php';
$request = &new cb_Request_Object();

$config_modules = cb_cache_get('modules', 'modules');

if (!is_admin()) {
    cb_check_maintenance ();

    if (isset($config_core['baned_ip']) && in_array(IP_ADDRESS, explode('|', $config_core['baned_ip']))) {
        show_message('对不起！您的IP地址(' . IP_ADDRESS . ')受限，不能访问本站！如有疑问，请与系统管理员联系');
    }

    if (isset($config_core['baned_agent']) && !empty($config_core['baned_agent'])) {
        $baned_agents = explode("|", $config_core['baned_agent']);
        foreach ($baned_agents as $baned_agent) {
            if (strpos($_SERVER['HTTP_USER_AGENT'], $baned_agent) !== false)
                show_message('您的客户端信息已经被网站屏蔽!如有疑问，请与我们联系!', INSTALL_PATH);
        }
    }

    $shortcuts = cb_cache_get('shortcut', 'menus');
    $tpl->assign('shortcuts', $shortcuts);
    unset($shortcuts);
}
if (!defined('MODULE_ID')) {
    $moduleid = $request->getModuleId();
    if (!$moduleid) {
        define('MODULE_ID', 1);
    } else {
        define('MODULE_ID', intval($moduleid));
    }
    unset($moduleid);
}
if (!isset($config_modules[MODULE_ID])) {
    cb_die('找不到相关模块的模型，您可能需要在后台重新安装相关模块，或重新安装《城市宝地方门户建站系统》', '初始化错误');
}
$config_module = $config_modules[MODULE_ID];
if ($config_modules[MODULE_ID]['ismenu'] == 1) {
    $config_modules[MODULE_ID]['current'] = 1;
} else {
    $config_modules[1]['current'] = 1;
}
$module = $config_module['module'];
$tpl->assign('config_module', $config_module);
$tpl->assign('config_modules', $config_modules);

if (is_admin() || !defined('NONUSER') || !NONUSER) {
    cb_init_user();
}

cb_init_logger();
